// pages/addNewbill/addNewbill.js
import api from "../../utils/api";
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userName: '',
        deptName: '',
        date: '', //创建时间
        ContractNo: '',
        TaskNumber: '',
        IntercourseUnit: '',
        IntercourseUnit_id: '',
        SignedAmount: '',
    },
    onContractNoChange(e) {
        this.setData({
            ContractNo: e.detail
        })
        wx.setStorageSync('ContractNo', this.data.ContractNo);
    },
    // onTaskNumberChange(e) {
    //     this.setData({
    //         TaskNumber: e.detail
    //     })
    //     wx.setStorageSync('TaskNumber',  this.data.TaskNumber);
    // },
    // 选择往来单位 跳转到页面
    iconclick(e) {
        wx.navigateTo({
            url: '/pages/intercourseUnit/intercourseUnit?from=0',
        })
    },
    onSignedAmountChange(e) {
        this.setData({
            SignedAmount: e.detail
        })
        console.log(this.data.SignedAmount)
        wx.setStorageSync('SignedAmount',  this.data.SignedAmount);
    },
    //点击保存,新增合同
    save() {
        if (this.data.ContractNo == '') {
            wx.showToast({
                title: '请先输入合同编号',
                icon: 'none'
            })
        }
        //  else if (this.data.TaskNumber == '') {
        //     wx.showToast({
        //         title: '请先输入合同名称',
        //         icon: 'none'
        //     })
        // } 
        else if (this.data.IntercourseUnit_id == '') {
            wx.showToast({
                title: '请选择签订单位',
                icon: 'none'
            })

        } else if (this.data.SignedAmount == '') {
            wx.showToast({
                title: '请先输入签订金额',
                icon: 'none'
            })
        } else {   
           let Ddate= this.formatDate(new Date().getTime())
            let that = this;
            api.addSysDictContractOne({
                method: 'POST',
                data: {
                    code: this.data.ContractNo,
                    name: this.data.TaskNumber,
                    money: this.data.SignedAmount,
                    cusId: this.data.IntercourseUnit_id,
                    coId: this.data.coId,
                    expDate:new Date().getTime()
                },
                success: function (res) {
                    console.log(res.data.data, "addSysDictContractOne")
                    if (res.data.code == 200) {
                        that.setData({
                          date:Ddate
                        })
                        wx.showToast({
                            title: '保存成功',
                        })
                        setTimeout(() => {
                            //跳转到选择合同号页面
                            wx.navigateBack({
                                delta:3,
                            })
                            wx.removeStorageSync('ContractNo')
                            // wx.removeStorageSync('TaskNumber')
                            wx.removeStorageSync('SignedAmount')
                        }, 1000);
                    } else {
                        wx.showModal({
                            content: res.data.message,
                            showCancel: false,
                            confirmColor: '#0041C4'
                        })
                    }
                }
            })
        }

    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options ,'超级超级')
        this.setData({
            IntercourseUnit_id: options.id,
            IntercourseUnit: options.name,
            ContractNo:options.ContractNo,
            // TaskNumber:options.TaskNumber,
            SignedAmount:options.SignedAmount,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.data.coId = wx.getStorageSync('userInfo').coId
        this.setData({
            userName: wx.getStorageSync('userInfo').unitName,
            deptName: wx.getStorageSync('userInfo').deptName,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
       
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    }
})